package com.iwuke.dance.wallet.balance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.iwuke.dance.R;
import com.iwuke.dance.gadget_view.StickyHeaderInterface;
import com.iwuke.dance.util.LogUtil;
import com.iwuke.dance.wallet.starlight_value.starlight_value_bean.StarlightTransactionRecordBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author: 魏从响
 * @date: 2025/9/25
 */
public class BalanceDetailsAdapter extends BaseQuickAdapter<StarlightTransactionRecordBean, BaseViewHolder> implements StickyHeaderInterface {

    private final Context context;

    public BalanceDetailsAdapter(Context context, int adapterECoinDetails, List<StarlightTransactionRecordBean> lists) {
        super(adapterECoinDetails, lists);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, StarlightTransactionRecordBean s) {
        RelativeLayout rlt_item_header = baseViewHolder.findView(R.id.rlt_item_header);
        TextView tv_date = baseViewHolder.findView(R.id.tv_date);//日期
        ImageView img_right = baseViewHolder.findView(R.id.img_right);//箭头旋转
        TextView tv_transfer_out_and_income = baseViewHolder.findView(R.id.tv_transfer_out_and_income);//转出和收入
        RecyclerView item_recycler_view = baseViewHolder.findView(R.id.item_recycler_view);//子列表
        TextView tv_no_data = baseViewHolder.findView(R.id.tv_no_data);//没有数据

        if (s.isHeader) {
            rlt_item_header.setVisibility(View.VISIBLE);
            tv_date.setText(s.month);//显示当月日期
            tv_transfer_out_and_income.setText("转出" + s.expense + "币\u3000收入" + s.income + "E币");//显示当月转出和收入的E币
        } else {
            rlt_item_header.setVisibility(View.GONE);
        }

        if (s.transactions!=null&&s.transactions.size()>0){
            item_recycler_view.setVisibility(View.VISIBLE);
            tv_no_data.setVisibility(View.GONE);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            item_recycler_view.setLayoutManager(linearLayoutManager);
            ItemBalanceDetailsAdapter adapter = new ItemBalanceDetailsAdapter(context, R.layout.adapter_item_balance_details, s.transactions);
            item_recycler_view.setAdapter(adapter);
        }else {
            item_recycler_view.setVisibility(View.GONE);
            tv_no_data.setVisibility(View.VISIBLE);
        }

        tv_date.setText(s.month);//显示当月日期
        tv_transfer_out_and_income.setText("支出¥"+s.expense+"\u3000收入¥"+s.income);//显示当月转出和收入的E币

//        img_right.setOnClickListener(new View.OnClickListener() {
//
//            private boolean isFag;//true 显示，false隐藏
//
//            @Override
//            public void onClick(View v) {
//                if (isFag){
//                    //隐藏
//                    item_recycler_view.setVisibility(View.GONE);
//                    img_right.setRotation(0);//旋转0度
//                    isFag = false;
//                }else {
//                    //显示
//                    item_recycler_view.setVisibility(View.VISIBLE);
//                    img_right.setRotation(90);//旋转九十度
//                    isFag = true;
//                }
//            }
//        });

    }

    // StickyHeaderInterface 实现
    @Override
    public boolean isHeader(int position) {
        return getItem(position).isHeader();
    }

    @Override
    public int getHeaderPositionForItem(int itemPosition) {
        for (int i = itemPosition; i >= 0; i--) {
            if (isHeader(i)) {
                return i;
            }
        }
        return RecyclerView.NO_POSITION;
    }

    @Override
    public View getHeaderView(RecyclerView parent, int headerPosition) {
        View headerView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_header, parent, false);
        TextView tv_date = headerView.findViewById(R.id.tv_date);//日期
        TextView tv_transfer_out_and_income = headerView.findViewById(R.id.tv_transfer_out_and_income);//转出和收入

        tv_date.setText(getItem(headerPosition).month);//显示当月日期
        tv_transfer_out_and_income.setText("转出" + getItem(headerPosition).expense + "币\u3000收入" + getItem(headerPosition).income + "E币");//显示当月转出和收入的E币

        return headerView;
    }

    @Override
    public void onHeaderClick(int position){
//        Toast.makeText(context, "点击了头部：", Toast.LENGTH_SHORT).show();
        LogUtil.logD("点击了头部",getItem(position).month);
    }

}
